/**
 * Created by hjx on 2018/3/6.
 */
(function () {
    var exports = this;

    var $ = null;
    /***
     *  config配置文件
     *  {
     *   selectBgRes:背景图
     *   selectBtnRes:下拉按钮图
     *   menuRes:菜单图 不支持9宫格 //菜单支持滑动
     *   dataArr:[] 数据
     *   normalIndex: 默认选中第几个数据
     *   fontSize: 字号大小
     *   selectCallBack: 选中后回调
     *   popDirection: 谈出方向
     *  }
     */


    exports.DropDownMenu = cc.Layer.extendEX({
        ctor: function (config) {
            this._super();

            var that = this;

            this._selectCallBack = config.selectCallBack;
            this._selectBg = new cc.Sprite(config.selectBgRes);
            this._transparent150X40 = new cc.Sprite(res.transparent150X40);
            this._selectBtn = new cc.Sprite(config.selectBtnRes);
            this._menuBg = new cc.Sprite(config.menuRes);
            this._menuBg.setAnchorPoint(cc.p(0.5, 1));
            this._selectBtn.x = this._selectBg.getContentSize().width * 0.4;
            this._menuBg.y = -(this._selectBg.getContentSize().height * 0.5 + 4);
            if (config.popDirection && config.popDirection == 'up') {
                this._menuBg.setAnchorPoint(cc.p(0.5, 0));
                this._menuBg.y = (this._selectBg.getContentSize().height * 0.5 + 4);
            }

            this.addChild(this._transparent150X40);
            this.addChild(this._selectBg);
            this.addChild(this._selectBtn);
            this.addChild(this._menuBg);
            this._menuBg.setVisible(false);

            var dataArr = config.dataArr;
            var fontSize = config.fontSize;
            this._selectLabel = new ccui.Text();
            this._selectLabel.setFontSize(fontSize);
            this._selectLabel.setString(dataArr[config.normalIndex || 0]);
            this._selectLabel.y = -4;
            this._selectLabel.x = -4;
            this.addChild(this._selectLabel);

            this._toucHLayer = new ccui.Layout();
            this._toucHLayer.x = -2000;
            this._toucHLayer.y = -2000;
            this._toucHLayer.width = 4000;
            this._toucHLayer.height = 4000;
            this._toucHLayer.setBackGroundColorOpacity(0);
            this.addChild(this._toucHLayer, -2);
            this._toucHLayer.setVisible(false);

            TouchUtil.setOnclickListener(this._toucHLayer, function () {
                that._menuBg.setVisible(false);
                that._toucHLayer.setVisible(false);
            }, {swallowTouches: true});
            TouchUtil.setOnclickListener(this._transparent150X40, function () {
                that._menuBg.setVisible(!that._menuBg.isVisible());
                that._toucHLayer.setVisible(that._menuBg.isVisible());
            }, {swallowTouches: false});
            TouchUtil.setOnclickListener(this._selectBtn, function () {
                that._menuBg.setVisible(!that._menuBg.isVisible());
                that._toucHLayer.setVisible(that._menuBg.isVisible());
            }, {swallowTouches: false});
            TouchUtil.setOnclickListener(this._menuBg, function () {
            });

            var contentSize = this._menuBg.getContentSize();
            this._scrollView = new ccui.ScrollView();
            this._scrollView.setContentSize(contentSize);
            this._scrollView.setTouchEnabled(true);
            this._scrollView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
            this._menuBg.addChild(this._scrollView);


            var cellHeight = fontSize + 12;
            if (dataArr.length >= 4) {
                cellHeight = fontSize + 9;
            }

            var viewHight = cellHeight * dataArr.length;
            var innerHeight = contentSize.height;
            if (viewHight >= innerHeight) {
                innerHeight = viewHight;
            }
            // this._scrollView.setColor(cc.color(125,125,125));
            // this._scrollView.setBackGroundColor(cc.color(125,125,125));
            // this._scrollView.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
            this._scrollView.setInnerContainerSize(cc.size(contentSize.width, viewHight));
            for (var i = 0; i < dataArr.length; i++) {
                var label = new ccui.Text();
                label.setFontSize(fontSize);
                label.setAnchorPoint(cc.p(0.5, 0));
                label.setString(dataArr[i]);
                var yy = (innerHeight - cellHeight * i - cellHeight);
                label.setPosition(cc.p(contentSize.width * 0.5, yy));
                this._scrollView.addChild(label);
                label.index = i;
                TouchUtil.setOnclickListener(label, this.selectLabelHandler.bind(this), {swallowTouches: false});
            }
        },

        selectLabelHandler: function (target) {
            var str = target.getString();
            var index = target.index;
            this._selectLabel.setString(str);
            if (this._selectCallBack) {
                this._selectCallBack(str, index);
            }
            this._menuBg.setVisible(false);
            this._toucHLayer.setVisible(false);
        },

    });

    exports.DropDownMenu = DropDownMenu;
})(window);